import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.geom.AffineTransform;
import java.awt.image.AffineTransformOp;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

/**
 * Created by berkbozkurt on 08/03/15.
 */
public class Paddle extends Rectangle {

    float x, y, lastX, lastY;
    int width, height;
    float xVelocity, yVelocity;
    float speed;
    float velocityConstant;
    Dimension border;
    private GameController logic;
    BufferedImage image;
    BufferedImage doubledImage;
    BufferedImage halvedImage;
    BufferedImage originalImage;
    boolean doubled;
    boolean halfed;

    public void paintComponent(Graphics g) {

    }
    public Paddle(Dimension border, String type)
    {

        doubled = false;
        halfed = false;
        logic = GameController.getInstance();

        this.border = border;
        if(type.equals("long")) {
            width = 150;
        }
        else if(type.equals("default")) {
            width = 100;
        }
        else {
            width = 75;
        }
        height = 20;
        lastX = x;
        lastY = y;
        xVelocity = speed;
        yVelocity = speed;
        velocityConstant = 1;
        setFrame(x-width/2, y-height/2, width, height);

        // Load images in constructor
        image = loadImage(type);
    }



    public void update(double delta)
    {
        setFrame(x-width/2, y-height/2, width, height);

        if(logic.isUpPressed()) {
            yVelocity = -2f;
        }
        if(logic.isDownPressed()) {
            yVelocity = 2f;
        }
        if(!logic.isDownPressed() && !logic.isUpPressed()) {
            yVelocity = 0;
        }

        if(logic.isRightPressed()) {
            xVelocity = 2f;
        }
        if(logic.isLeftPressed()) {
            xVelocity = -2f;
        }
        if(!logic.isRightPressed() && !logic.isLeftPressed()) {
            xVelocity = 0;
        }

        lastX = x;
        lastY = y;

        x += velocityConstant * xVelocity * delta;
        y += velocityConstant * yVelocity * delta;

        if (x + width/2 >= border.getSize().getWidth())
        {
            xVelocity = 0;
            x = (float) border.getWidth() - width/2;
            //yVelocity = yVelocity * 0.95f; // Decrease speed by %5
        }
        else if (x - width/2 <= 0)
        {
            xVelocity = 0;
            x = width/2;
        }

        if (y + height/2 >= border.getSize().getHeight())
        {

            yVelocity = 0;
            y = (float) border.getHeight() - height/2;
            //xVelocity = xVelocity * 0.95f; // Decrease speed by %5
        }
        else if (y - height/2 <= border.getSize().getHeight() * 0.6)
        {
            yVelocity = 0;
            y = (float) border.getSize().getHeight() * 0.6f + height/2;
        }

    }

    public BufferedImage getImage() {
        if(halfed)
            return halvedImage;

        else if(doubled)
            return doubledImage;

        return image;
    }
    public BufferedImage loadImage(String type) {
        String path = null;
        if(type.equals("long"))
            path = "resources/longerPlayer.png";
        else if(type.equals("default")) {
            path = "resources/player.png";
        }
        else {
            path = "resources/playerHalf.png";
        }

        BufferedImage img = null;
        try {
            img = ImageIO.read(new File(path));
        } catch (IOException e) {
            System.out.println("Cant find img");
            e.printStackTrace();
        }
        return img;

    }


    public void applyEffect(String type) {
        switch(type) {
            case "fasterPaddle":
                velocityConstant *= 1.2f;
                break;
            case "slowerPaddle":
                if(velocityConstant > 0.4f)
                    velocityConstant *= 0.8f;
                break;
            case "paddleGun":

                break;
            case "magnetizedPaddle":

                break;
            case "invertedPaddle":
                break;
            default:
                break;
        }
    }


}
